package com.jcfk.system.service;

import com.jcfk.system.domain.po.SysPrintContentSetting;
import com.jcfk.system.domain.query.SysPrintContentSettingQuery;
import com.jcfk.system.domain.dto.SysPrintContentSettingDTO;
import com.jcfk.system.dao.SysPrintContentSettingDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.jcfk.common.core.utils.IdUtils;
import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 打印内容设置
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-01-09
 */
@Service
public class SysPrintContentSettingService extends BaseService<SysPrintContentSettingDao,SysPrintContentSetting> {

    /**
     * SysPrintContentSetting分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<SysPrintContentSettingDTO> queryByPage(DataPage<SysPrintContentSettingDTO> page, SysPrintContentSettingQuery query) {
        // 分页查询
        query.setContentName(StringUtils.isEmpty(query.getContentName()) ? query.getContentName() : query.getContentName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断SysPrintContentSetting是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(SysPrintContentSettingDTO dto) {
        QueryWrapper<SysPrintContentSetting> queryWrapper = new QueryWrapper<>();
                                                                    
        return RetResult.ok();
    }

    /**
     * 插入打印内容设置
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysPrintContentSettingDTO dto) {

        // 保存
        SysPrintContentSetting item = new SysPrintContentSetting();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新打印内容设置
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SysPrintContentSettingDTO dto) {

        // 保存
        SysPrintContentSetting item = new SysPrintContentSetting();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(SysPrintContentSetting item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

    }

    /**
     * 根据主键获取打印内容设置信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public SysPrintContentSettingDTO get(String id) {
        // 查询数据
        SysPrintContentSetting item = this.baseMapper.selectById(id);
            SysPrintContentSettingDTO dto = new SysPrintContentSettingDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SysPrintContentSettingDTO>
     */
    public List<SysPrintContentSettingDTO> getList(SysPrintContentSettingQuery query) {
        // 查询条件
        QueryWrapper<SysPrintContentSetting> qw = new QueryWrapper<>();
                                                                            qw.lambda().eq(!StrUtil.isEmpty(query.getContentCode()), SysPrintContentSetting::getContentCode, query.getContentCode());
                                                qw.lambda().likeRight(!StrUtil.isEmpty(query.getContentName()), SysPrintContentSetting::getContentName, query.getContentName());
                                                qw.lambda().eq(!StrUtil.isEmpty(query.getPrintMode()), SysPrintContentSetting::getPrintMode, query.getPrintMode());
                                                                                                                                                                                                                                                                            // 查询数据
        List<SysPrintContentSetting> list = super.list(qw);

        return convertList(list, SysPrintContentSettingDTO.class);
    }
}

